package com.erp.fin.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.erp.common.controller.BaseController;
import com.erp.common.utils.*;
import com.erp.itm.domain.ProjectDO;
import com.erp.itm.service.ProjectService;
import com.erp.oa.domain.NotifyDO;
import com.erp.oa.service.NotifyService;
import com.erp.system.domain.CompanyDO;
import com.erp.system.domain.UserDO;
import com.erp.system.service.CompanyService;
import com.erp.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.fin.domain.ReceiptDO;
import com.erp.fin.service.ReceiptService;

import javax.servlet.http.HttpServletResponse;

/**
 * 收款管理
 * 
 * @author Eric
 * @email shiliang900713@163.com
 * @date 2019-08-28 14:13:31
 */
 
@Controller
@RequestMapping("/fin/receipt")
public class ReceiptController extends BaseController {
	@Autowired
	private ReceiptService receiptService;

	@Autowired
	private ProjectService projectService;

	@Autowired
	private CompanyService companyService;

	@Autowired
	private UserService userService;
	@Autowired
	private NotifyService notifyService;
	@GetMapping()
	@RequiresPermissions("fin:receipt:receipt")
	String Receipt(){
	    return "fin/receipt/receipt";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("fin:receipt:receipt")
	public PageUtils list(@RequestParam Map<String, Object> params){
		if(getUser().getDeptId() != 16 && getUser().getDeptId() !=17 && getUser().getDeptId() !=23){
			params.put("projectIds",getUserId());
		}
		//查询列表数据
        Query query = new Query(params);
		List<ReceiptDO> receiptList = receiptService.list(query);
		int total = receiptService.count(query);
		PageUtils pageUtils = new PageUtils(receiptList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("fin:receipt:add")
	String add(){
	    return "fin/receipt/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("fin:receipt:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ReceiptDO receipt = receiptService.get(id);
		model.addAttribute("receipt", receipt);
		//查询收款方
		List<CompanyDO> companyDOList = companyService.list(null);
		model.addAttribute("companyDOList", companyDOList);
		//查询项目
		List<ProjectDO> projectDOList = projectService.getNotEndProjectlist();
		model.addAttribute("projectDOList", projectDOList);
	    return "fin/receipt/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("fin:receipt:add")
	public R save( ReceiptDO receipt){
		UserDO userDO = ShiroUtils.getUser();
		receipt.setStatus(1);
		//查询项目名称和编号
		ProjectDO projectDO = projectService.get(receipt.getProjectId());
		if(projectDO != null){
			receipt.setProjectName(projectDO.getName());
			receipt.setProjectNumber(projectDO.getItmNumber());
		}
		//查询收款公司信息
		CompanyDO companyDO = companyService.get(Integer.valueOf(receipt.getRecriptCompanyid()+""));
		if(companyDO != null){
			receipt.setRecriptCompanyname(companyDO.getName());
		}
		receipt.setUserIdCreate(userDO.getUserId());
		receipt.setUserNameCreate(userDO.getName());
		receipt.setGmtCreate(new Date());
		if(receiptService.save(receipt)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("fin:receipt:edit")
	public R update( ReceiptDO receipt){
		//查询项目名称和编号
		ProjectDO projectDO = projectService.get(receipt.getProjectId());
		if(projectDO != null){
			receipt.setProjectName(projectDO.getName());
			receipt.setProjectNumber(projectDO.getItmNumber());
		}
		//查询收款公司信息
		CompanyDO companyDO = companyService.get(Integer.valueOf(receipt.getRecriptCompanyid()+""));
		if(companyDO != null){
			receipt.setRecriptCompanyname(companyDO.getName());
		}
		receiptService.update(receipt);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("fin:receipt:remove")
	public R remove( Long id){
		if(receiptService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("fin:receipt:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		receiptService.batchRemove(ids);
		return R.ok();
	}

	/**
	 * 确认收款
	 * @param id
	 * @return
	 */
	@PostMapping( "/cofirmAmount")
	@ResponseBody
	public R cofirmAmount(Long id){
		if(receiptService.cofirmAmount(id)>0){
			//更新收款金额
			ReceiptDO receiptDO = receiptService.get(id);
			projectService.updatereceivedAmount(receiptDO.getReceiptAmount(),receiptDO.getProjectId());

			//通知
			NotifyDO notify = new NotifyDO();
			notify.setType("8");
			notify.setTitle("收款登记");
			List<Long> deprids = new ArrayList<>();
			deprids.add(23L);
			deprids.add(17L);
			deprids.add(16L);
			List<UserDO> userDOList =userService.getUsersByDeptIds(deprids);
			List<Long> userId = userDOList.stream().map(UserDO::getUserId).collect(Collectors.toList());
			notify.setContent("项目编号为["+receiptDO.getProjectNumber()+"]名称为["+receiptDO.getProjectName()+"]收到一笔款项，" +
					"金额为["+receiptDO.getReceiptAmount()+"]");
			//查询项目添加人
			ProjectDO projectDO = projectService.get(receiptDO.getProjectId());
			userId.add(projectDO.getUserIdCreate());
			Long[] userIds =userIds = userId.toArray( new Long[userId.size()]);
			notify.setUserIds(userIds);
			notify.setStatus("1");
			notify.setCreateBy(getUserId());
			notifyService.save(notify);

			return R.ok();
		}
		return R.error();
	}


	@GetMapping("/getRecpidByitm/{id}")
	String getRecpidByitm(@PathVariable("id") Long id,Model model){
		List<ReceiptDO> receiptList = receiptService.listByitm(id);
		model.addAttribute("receiptList", receiptList);
		model.addAttribute("itmid", id);
		return "fin/receipt/receiptbyitm";
	}


	@PostMapping("/exportData")
	@ResponseBody
	public void exportData( Long itmid, HttpServletResponse response){
		List<ReceiptDO> receiptList = receiptService.listByitm(itmid);
		ExcelUtil.exportExcel(receiptList,"已收金额列表","已收金额列表",ReceiptDO.class,"已收金额列表.xls",response);
	}
	@PostMapping("/exportDataByList")
	@ResponseBody
	public void exportDataByList( ReceiptDO receiptDO, HttpServletResponse response){

		Map<String, Object> params = new HashMap<>();
		if(StringUtils.isNotBlank(receiptDO.getProjectName())){
			params.put("projectName",receiptDO.getProjectName());
		}
		if(StringUtils.isNotBlank(receiptDO.getProjectNumber())){
			params.put("projectNumber",receiptDO.getProjectNumber());
		}
		if(StringUtils.isNotBlank(receiptDO.getRecriptCompanyname())){
			params.put("recriptCompanyname",receiptDO.getRecriptCompanyname());
		}
		if(receiptDO.getReceiptDateend() != null){
			params.put("receiptDateend",receiptDO.getReceiptDateend());
		}
		if(receiptDO.getReceiptDateStr() != null){
			params.put("receiptDateStr",receiptDO.getReceiptDateStr());
		}
		List<ReceiptDO> receiptList = receiptService.list(params);
		ExcelUtil.exportExcel(receiptList,"已收金额列表","已收金额列表",ReceiptDO.class,"已收金额列表.xls",response);
	}
}
